//
//  AXHdzqMsZzfqVC.m
//  AnXin100
//
//  Created by 天质弘耕 on 2018/11/1.
//  Copyright © 2018 tzhg. All rights reserved.
//

#import "AXHdzqMsZzfqVC.h"
#import "UIView+SDAutoLayout.h"
#import "ZXCategories.h"
#import "Header.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "AXHdzqMsZzfqCell.h"
#import "AXHdzqMsZzfqListModel.h"
#import "AXHdzqMsZzfqDetailVC.h"
#import "CFDetailInfoController.h"

@interface AXHdzqMsZzfqVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray<AXHdzqMsZzfqListModel *>  *modelsArray;
/** 假图片数据 */
@property (nonatomic, strong) NSArray<NSString *> *imageNames;
@property (nonatomic, strong) NSArray<NSString *> *titleNames;
@property (nonatomic, strong) NSArray<NSString *> *storeNames;
@property (nonatomic , assign) NSInteger requstIndex;


@end

@implementation AXHdzqMsZzfqVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self creatView];
    [self setlayout];
    [self setupNavigationItems];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self requestList];
       
    }];
    
    
    // 上拉刷新
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self requestList];
    }];
    
    LKEmptyViewConfig *config = [LKEmptyViewConfig setupEmptyViewConfigForTableView:self.tableView];
    config.message = @"暂无数据";

    [self.tableView.mj_header beginRefreshing];
    
}




- (void)creatView {
    /** 表头创建 */
    self.tableView = [[UITableView alloc]init];
    /** 去掉cell线 */
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[AXHdzqMsZzfqCell class] forCellReuseIdentifier:[AXHdzqMsZzfqCell reuseID]];
    [self.view addSubview:self.tableView];
    
}



- (void)setlayout {
    
    self.tableView.sd_layout
    .leftSpaceToView(self.view, 0)
    .topSpaceToView(self.view, 0)
    .widthIs(Wi)
    .heightIs(MyTableViewHeight-44);
    
    
}



- (void)setupNavigationItems {
    
    /** 导航条左边的按钮 */
    //返回按钮
    UIButton *kfBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [kfBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    kfBtn.frame = CGRectMake(0, 0, 30, 30);
    [kfBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithCustomView:kfBtn];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    /** title */
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 30)];
    titleLabel.text = @"正在疯抢";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = FONT_EIGHTEEN;
    titleLabel.textColor = [UIColor colorWithHex:0x29b566];
    self.navigationItem.titleView = titleLabel;
    
    
}

#pragma mark - requestNetwork

- (void)requestList {
    
    MBProgressHUD *hud = [WHRNetwork showHudAtView:self.view message:@"正在加载..." isTextOnly:NO];
    ZX_WEAK_SELF
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"actitvityListType"] = @"UNDERWAY";
    params[@"pageNum"] = @1;
    [NetWorkHelp yl_postDataWith:params url:URL_SPLIT(URL_ActivityPrefecture, URL_ActivityPrefecture_getSeckillListByMmallSeckill) success:^(id respose) {
        
        
        if ([respose[@"header"][@"statusCode"] integerValue] == 200) {
            
            weakSelf.modelsArray = [AXHdzqMsZzfqListModel mj_objectArrayWithKeyValuesArray:respose[@"body"][@"seckillListVOs"]];
            [self.tableView reloadData];
            
        } else if ([respose[@"header"][@"statusCode"] integerValue] != 200) {
            
            
        }
        
         [hud hideAnimated:YES];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
        
    } failure:^(NSError *error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    }];
    
}


#pragma mark - Action

- (void)backAction:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)search:(UIButton *)btn {
    
}



#pragma mark - tableViewDelegate & tableViewDatasorce

/** cell 的选中方法 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    AXHdzqMsZzfqListModel *model = self.modelsArray[indexPath.row];
    AXHdzqMsZzfqDetailVC *vc = [[AXHdzqMsZzfqDetailVC alloc]init];
    /** 列表界面传一个商品id到详情界面 */
    vc.shopId = model.shopId;
    vc.goodsId = model.goodsId;
    /** 秒杀价，直接传到详情页面 */
    vc.skillPrice= model.seckillPrice;
    /** 规格id */
    vc.specificationId = model.formatId;
    /** 活动结束时间传到下一个界面 */
    vc.fallTime = model.endDatetime ;
    
    
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:vc animated:YES];
    
}


//几个分区
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.modelsArray.count;
}

/** cell的高度 */

/** cell 自适应第二步 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return [tableView cellHeightForIndexPath:indexPath model:self.modelsArray[indexPath.row] keyPath:@"model" cellClass:[AXHdzqMsZzfqCell class] contentViewWidth:SCREEN_WIDTH];
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    
    
    AXHdzqMsZzfqCell *cell = [tableView dequeueReusableCellWithIdentifier:[AXHdzqMsZzfqCell reuseID] forIndexPath:indexPath];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    cell.model  = self.modelsArray[indexPath.row];
    
    cell.tapCelllickedBlock = ^(NSInteger index) {
      
        CFDetailInfoController *vc = [[CFDetailInfoController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    };
    
    
    return cell;
    
    
}





@end

